package com.myhexin.demo.ignite.test.base;

import com.myhexin.demo.ignite.model.po.Person;
import org.apache.ignite.Ignition;
import org.apache.ignite.cache.query.Query;
import org.apache.ignite.cache.query.QueryCursor;
import org.apache.ignite.cache.query.ScanQuery;
import org.apache.ignite.client.ClientCache;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.configuration.ClientConfiguration;

import javax.cache.Cache;

/**
 * @author zhangzhonglv@myhexin.com
 * @date 2025/8/15
 */
public class ScanQueryDemo {

    private static final String IGNITE_ADDR = "192.168.211.106:10800";

    public static void main(String[] args) {
        ClientConfiguration cfg = new ClientConfiguration().setAddresses(IGNITE_ADDR);
        cfg.setPartitionAwarenessEnabled(true);
        try (IgniteClient client = Ignition.startClient(cfg)) {
            ClientCache<Integer, Person> personCache = client.getOrCreateCache("personCache");
            personCache.put(1, new Person(1, "Jerry"));
            personCache.put(2, new Person(2, "Tom"));
            personCache.put(3, new Person(3, "Smith"));
            personCache.put(4, new Person(4, "Smith2"));

             Query<Cache.Entry<Integer, Person>> qry = new ScanQuery<Integer, Person>(
                     (i, p) -> p.getName().contains("Smith"));
            // TODO 这里使用过滤就会报错，目前不清楚怎么回事
//            Query<Cache.Entry<Integer, Person>> qry = new ScanQuery<Integer, Person>();

            try (QueryCursor<Cache.Entry<Integer, Person>> cur = personCache.query(qry)) {
                for (Cache.Entry<Integer, Person> entry : cur) {
                    // Process the entry ...
                    System.out.println("entry:" + entry.getValue());
                }
            }
        }
    }
}
